package com.veryon9.mobile.model;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;

import android.util.Log;

public class SlowForumDataSource {
	
	public static List<String> topicOptionsList = new ArrayList<String>();
	
	private static TagNode fetchHtmlByUrl(URL aUrl)
	{
    	CleanerProperties props = new CleanerProperties();
        
 	     // set some properties to non-default values
 	     props.setTranslateSpecialEntities(true);
 	     props.setTransResCharsToNCR(true);
 	     props.setOmitComments(true);
 	     TagNode tagNode = null;
 	     // do parsing
 	     int sn = 1;
 	     while (tagNode == null)
	   	 {
		  	     try {
		  	    	 	
			  			tagNode = new HtmlCleaner(props).clean(aUrl);
			  			
		  		} catch (MalformedURLException e) {
		  			// TODO Auto-generated catch block
		  			e.printStackTrace();
		  		} catch (IOException e) {
		  			// TODO Auto-generated catch block
		  			e.printStackTrace();
		  		}
		  		sn++;
		  		if (sn > 9)
		  			sn = 1;
	   	 }
 	     return tagNode;
	}
	
	public static List<Map<String, String>> getPostData(URL aUrl) {

		TagNode tagNode = fetchHtmlByUrl(aUrl);
		
		TagNode[] topicNode = tagNode.getElementsByAttValue("class", "repliers", true, false);
		List<Map<String, String>> contents = new ArrayList<Map<String, String>>();
		
		for (TagNode p : topicNode)
		{
			TagNode tr = p.findElementHavingAttribute("userid", true);
		
			Map<String, String> map = new HashMap<String, String>();
			map.put("USERNAME", tr.getAttributeByName("username"));
			map.put("USERID", tr.getAttributeByName("userid"));
			TagNode link = tr.findElementByAttValue("class", "repliers_right", true, false);
			link = link.findElementByName("td", true);
			map.put("CONTENT", link.getText().toString());
			contents.add(map);
		}
		       
		try {
		    Thread.sleep(0);
		} catch (InterruptedException e) {
		    Log.e("SlowDataSource", "interrupted", e);
		}
		
		return contents;
    }  

    public static List<Map<String, String>> getTopicData() {

  	    TagNode tagNode = null;
		try {
			tagNode = fetchHtmlByUrl(new URL("http://forum9.hkgolden.com/topics.aspx?type=BW&filtermode=N"));
		} catch (MalformedURLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

  		TagNode topicNode = tagNode.findElementByAttValue("class", "Topic_ListPanel", true, false);
  		
  		TagNode topicOptionNode = tagNode.findElementByAttValue("class", "Topic_FunctionPanel", true, false);
  		
  		List<TagNode> optionList = topicOptionNode.getElementListByName("option", true);
  		
  		if (SlowForumDataSource.topicOptionsList.size() == 0)
  		{
	  		for (TagNode on : optionList)
	  		{
	  			SlowForumDataSource.topicOptionsList.clear();
	  			if (!on.hasAttribute("selected"))
	  				SlowForumDataSource.topicOptionsList.add(on.getAttributeByName("value"));
	  		}
  		}

  		TagNode[] trs = topicNode.getElementsHavingAttribute("userid", true);
  		
  		List<Map<String, String>> contents = new ArrayList<Map<String, String>>();
  		
  		for (TagNode tr : trs)
  		{
			Map<String, String> map = new HashMap<String, String>();
			map.put("USERNAME", tr.getAttributeByName("username"));
			map.put("USERID", tr.getAttributeByName("userid"));
			TagNode[] links = tr.getElementsHavingAttribute("href", true);
			for (TagNode link : links)
			{
				if (link.getAttributeByName("href").contains("view.aspx?type=BW"))
		  		{
					map.put("TITLE", link.getText().toString());
				
					map.put("LINK", link.getAttributeByName("href"));
	  			}
			}
			contents.add(map);
		}
               
        try {
            Thread.sleep(0);
        } catch (InterruptedException e) {
            Log.e("SlowDataSource", "interrupted", e);
        }
       
        return contents;
    }  

}
